package com.xzmzhp.controller.cos;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.cos.OrnamentsType;
import com.xzmzhp.service.cos.OrnamentsTypeService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-24 15:02
 * @program: ams
 * @description: cos服配件类型前端交互类
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/ornamentsType")
public class OrnamentsTypeController {

    @Autowired
    private OrnamentsTypeService ornamentsTypeService;

    /**
     * 查询所有cos配件类型信息
     * @param param 参数
     *        ornamentsTypeStatus cos服配件类型状态
     *        ornamentsTypeName cos服配件类型名称
     * @return cos配件类型信息
     */
    @RequestMapping("/findOrnamentsTypeList")
    public ReturnData findOrnamentsTypeList(@RequestParam(value = "page", defaultValue = "1") int page,
                                             @RequestParam(value = "limit", defaultValue = "10") int limit,
                                             @RequestBody Map<String,Object> param){
        PageHelper.startPage(page, limit, true);
        List<OrnamentsType> list = ornamentsTypeService.findOrnamentsTypeList(param);
        if (CollUtil.isNotEmpty(list)){
            PageInfo<OrnamentsType> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询cos服配件类型信息失败,请重新查询");
    }

    /**
     * 验证cos配件类型是否存在
     *
     * @param ornamentsTypeName cos配件类型名称
     * @return cos配件类型信息
     */
    @RequestMapping("/checkOrnamentsTypeByName")
    public ReturnData checkOrnamentsTypeByName(String ornamentsTypeName){
        OrnamentsType ornamentsType = ornamentsTypeService.checkOrnamentsTypeByName(ornamentsTypeName);
        if (StringUtils.isNotNull(ornamentsType)) {
            return ReturnData.ok(ornamentsType);
        }
        return ReturnData.error(ReturnConstant.FAIL, "cos服配件类型信息添加错误");
    }

    /**
     * 添加节目类型信息
     * @param ornamentsType 节目类型信息
     * @return 是否成功
     */
    @RequestMapping("/addProgramType")
    public ReturnData addProgramType(@RequestBody OrnamentsType ornamentsType){
        Integer i = ornamentsTypeService.addOrnamentsType(ornamentsType);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        }
        return ReturnData.error(ReturnConstant.FAIL, "cos服配件类型添加错误");
    }

    /**
     * 修改节目类型信息
     * @param ornamentsType 节目类型信息
     * @return 是否成功
     */
    @RequestMapping("/exitProgramType")
    public ReturnData exitProgramType(@RequestBody OrnamentsType ornamentsType){
        Integer i = ornamentsTypeService.updateOrnamentsType(ornamentsType);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "cos服配件类型修改错误");
        }
    }

    /**
     * 根据cos服配件类型id修改cos服配件类型状态
     * @param ornamentsTypeId cos服配件类型id
     * @param status cos服配件类型状态
     * @return 是否成功
     */
    @RequestMapping("/exitStatus")
    public ReturnData exitStatus(@RequestParam(value = "ornamentsTypeId")Integer ornamentsTypeId,
                                 @RequestParam(value = "status") String status){
        Integer i = ornamentsTypeService.exitStatus(ornamentsTypeId,status);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        }
        return ReturnData.error(ReturnConstant.FAIL, "cos服配件类型修改错误");
    }
}
